package com.gaswell.canal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaswell.canal.chain.PoliceHandler;
import com.gaswell.entity.Qba01;
import com.gaswell.entity.Qfc01;
import com.gaswell.mapper.AlarmRecordMapper;
import com.gaswell.pojo.ALLQj;
import com.gaswell.pojo.Diagnosis;
import com.gaswell.pojo.ReciveCycleData;
import com.gaswell.service.ALLQjService;
import com.gaswell.service.IQfc01Service;
import com.gaswell.service.impl.Qfc01ServiceImpl;
import com.gaswell.utils.RedisEnumeration;
import com.gaswell.utils.StatusDetect;
import com.gaswell.vo.Qba01Count;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@CanalTable(value = "qba01")
public class Qba01CountHandler implements EntryHandler<Qba01> {
    @Autowired
    BeanFactory beanFactory;
    @Autowired
    AlarmRecordMapper alarmRecordMapper;

    @Autowired
    PoliceHandler policeHandler;

    @Autowired
    IQfc01Service qfc01Service;

    @Autowired
    ALLQjService allQjService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 用于检测日数据变化，并及时更新缓存内容
     * @param qba01
     */

    @Override
    public void insert(Qba01 qba01) {
        String jh = qba01.getJh();
        ALLQj qj = allQjService.findOne(jh);
        if (qj != null) {
            String ym = qj.getYm();

//          如果存在缓存数据，进行添加
            String qba01CountKey = RedisEnumeration.getQba01Count(ym);
            if (redisTemplate.hasKey(RedisEnumeration.getQba01Count(ym))) {
                LambdaQueryWrapper<Qfc01> qfc01LambdaQueryWrapper = new LambdaQueryWrapper<>();
                qfc01LambdaQueryWrapper.eq(Qfc01::getJh, jh)
                        .eq(Qfc01::getQyrq, qba01.getRq().toString());
                Qfc01 one = qfc01Service.getOne(qfc01LambdaQueryWrapper);
                double g = one.getTrqxdmd().doubleValue();
                double d = 62;
                double vg = 2.5;


//              检查有没有问题，有问题在添加
                ReciveCycleData reciveCycleData = new ReciveCycleData()
                        .setJkwd(qba01.getJkwd() == null ? 0 : qba01.getJkwd().doubleValue())
                        .setTgyl(qba01.getPjty() == null ? 0 : qba01.getPjty().doubleValue())
                        .setYgyl(qba01.getPjyy() == null ? 0 : qba01.getPjyy().doubleValue())
                        .setWsyl(qba01.getWsyl() == null ? 0 : qba01.getWsyl().doubleValue())
                        .setCqill(qba01.getRcql() == null ? 0 : qba01.getRcql().doubleValue());
                String jiYe = StatusDetect.isJiYe(reciveCycleData, d, g, vg);
                String shuiHeWu = StatusDetect.isShuiHeWu(reciveCycleData, g);


//              出现问题了
                if (jiYe.equals("1") || shuiHeWu.equals("1")) {
                    List<Qba01Count> range = redisTemplate.opsForList().range(qba01CountKey, 0, -1);
                    int index = -1;
                    for (int i = 0; i < range.size(); i++) {
                        if (range.get(i).getJh().equals(qba01.getJh())) {
                            index = i;
                            break;
                        }
                    }
//                  判断问题
                    StringBuilder conditionValue = new StringBuilder();
                    if (jiYe.equals("1"))
                        conditionValue.append("积液");
                    if (shuiHeWu.equals("1"))
                        conditionValue.append("冻堵");

//                    用来添加新数据
                    //                  代表原来就有数据
                    if (index != -1) {
                        Qba01Count qba01Count = range.get(index);

                        qba01Count.getDates().add(qba01.getRq().toString());

                        qba01Count.getConditions().add(conditionValue.toString());
                        range.set(index, qba01Count);
                    } else {
//                   原来缓存中没有数据
                        Qba01Count qba01Count = new Qba01Count();
                        ArrayList<String> conditions = new ArrayList<>();
                        ArrayList<String> dates = new ArrayList<>();
                        conditions.add(conditionValue.toString());
                        dates.add(qba01.getRq().toString());
                        qba01Count.setConditions(conditions);
                        qba01Count.setDates(dates);
                        range.add(qba01Count);
                    }


                    redisTemplate.delete(qba01CountKey);
                    redisTemplate.opsForList().rightPushAll(range);
                }
            }
        }


    }

//    @Override
//    public void update(Diagnosis before, Diagnosis after) {
//        policeHandler.handleRequest(after);
//        policeHandler.handleRequest(before);
//    }
//
//    @Override
//    public void delete(Diagnosis diagnosis) {
//        policeHandler.handleRequest(diagnosis);
//    }
}
